package com.rhb.erp.hr.department.web.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.*;

import com.rhb.erp.hr.department.bean.*;
import com.rhb.erp.hr.department.web.struts.form.*;
import com.rhb.erp.web.struts.action.ErpBaseDispatchAction;
import com.rhb.exception.CanNotBeDeletedException;
import com.rhb.exception.CircleException;
import com.rhb.exception.DuplicateException;

public class DepartmentSaveAction extends ErpBaseDispatchAction {
	public ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		return mapping.findForward("fail");
	}
	
	public ActionForward del(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		DepartmentForm oform = (DepartmentForm)form;
		ActionMessages messages = new ActionMessages();
		try {
			this.getDepartmentBusiness().delete(oform.getDepartment());
		} catch (CanNotBeDeletedException e) {
			messages.add("errors.noDelete", new ActionMessage("errors.noDelete",e.toString()));
			addErrors(request, messages);
			return mapping.findForward("failDel");
		}
		return mapping.findForward("successDel");
	}
	
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		DepartmentForm oform = (DepartmentForm)form;
		
		if(oform.getParentPk()!=null && !"".equals(oform.getParentPk())){
			Department parent = new Department();
			parent.setDepartmentPk(oform.getParentPk());
			parent.setDepartmentName(oform.getParentName());
			oform.getDepartment().setParent(parent);
		}else{
			oform.getDepartment().setParent(null);
		}
		
		ActionMessages messages = new ActionMessages();
		try {
			this.getDepartmentBusiness().update(oform.getDepartment());
		} catch (DuplicateException e) {
			messages.add("errors.duplicate", new ActionMessage("errors.duplicate",e.toString()));
			addErrors(request, messages);
			return new ActionForward(mapping.getInput());
		} catch (CircleException e) {
			messages.add("errors.circle", new ActionMessage("errors.circle",e.toString()));
			addErrors(request, messages);
			return mapping.findForward("fail");
		}
		return mapping.findForward("success");
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		DepartmentForm oform = (DepartmentForm)form;
		
		if(oform.getParentPk()!=null && !"".equals(oform.getParentPk())){
			Department parent = new Department();
			parent.setDepartmentPk(oform.getParentPk());
			parent.setDepartmentName(oform.getParentName());
			oform.getDepartment().setParent(parent);
		}
		
		ActionMessages messages = new ActionMessages();
		try {
			this.getDepartmentBusiness().create(oform.getDepartment());
		} catch (DuplicateException e) {
			messages.add("errors.duplicate", new ActionMessage("errors.duplicate",e.toString()));
			addErrors(request, messages);
			return mapping.findForward("fail");
		} catch (CircleException e) {
			messages.add("errors.circle", new ActionMessage("errors.circle",e.toString()));
			addErrors(request, messages);
			return mapping.findForward("fail");
		}
		return mapping.findForward("fail");
	}	 
}
